package gui;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreePath;

import business.FormationBL;
import business.SeanceBL;

public class SeanceServiceUI extends JPanel {
	
	private int seanceId;
	private int serviceId;
	private int periodeId;
	
//	private SemaineServiceUI semaine;
	
	private AbstractPopupController popupController = new AbstractPopupController() {
		public JPopupMenu makePopup(int x, int y) {
			return new Popup();
		}
	};
	
	public SeanceServiceUI(int seanId, Color color, String nom, String type, String prof, String promo, String groupe, String salle) {
		
//		this.semaine = semaine;
		seanceId = seanId;
		
		SeanceBL seanceBL = new SeanceBL(seanceId);
		serviceId = seanceBL.getIdService();
		periodeId = seanceBL.getIdPeriode();
		
		this.setLayout(new GridLayout(3,0));
		this.setBackground(color);
		this.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.BLACK));

		this.addMouseListener(popupController);
		
		JLabel nomLabel = new JLabel(nom); nomLabel.setHorizontalAlignment(SwingConstants.CENTER); add(nomLabel);
		JLabel salleLabel = new JLabel(salle); salleLabel.setHorizontalAlignment(SwingConstants.CENTER); add(salleLabel);
		JLabel profLabel = new JLabel(prof); profLabel.setHorizontalAlignment(SwingConstants.CENTER); add(profLabel);
		
		this.setToolTipText(type + "\n" + promo + (groupe.isEmpty() ? "" : (" grp." + groupe)));
	}

	public void setColor(Color color) {
		this.setBackground(color);
	}
	
	public void hideSeance() {
		this.setVisible(false);
	}
	
	public int getPeriodeId() {
		SeanceBL seanceBL = new SeanceBL(seanceId);
		return seanceBL.getIdPeriode();
	}
	
	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem modifier = new JMenuItem("Modifier");
		JMenuItem supprimer = new JMenuItem("Supprimer");
		
		public Popup() {
			modifier.addActionListener(this);
			supprimer.addActionListener(this);
			add(modifier);
			add(supprimer);
		}
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == modifier) {
				FrameAjoutSeance frameAjout = new FrameAjoutSeance(null, seanceId, serviceId, periodeId, false);
				frameAjout.setVisible(true);
			} else if (e.getSource() == supprimer) {
				int retour = JOptionPane.showConfirmDialog(
					    this,
					    "Êtes vous sûr de vouloir supprimer la séance\n"+
					    //ensBL.getName()+" "+ensBL.getPrenom()+
					    " ?",
					    "Avertissement",
					    JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
				if (retour == JOptionPane.YES_OPTION) {
					SeanceBL seanceBL = new SeanceBL(seanceId);
					seanceBL.supprimerSeance();
					hideSeance();
				}

			}
			
		}
		
	}
	
}
